<?php

namespace app\admin\validate\shop\order;

use app\shop\enums\order\refund\RefundAuditStatus;
use think\Validate;

class Refund extends Validate
{
    protected $failException = true;

    /**
     * 验证规则
     */
    protected $rule = [
        'id'                  => 'require',
        'refund_type'         => 'require',
        'audit_status'        => 'require',
        'refund_address_id'   => 'requireAddressId',
        'refuse_reason'       => 'requireRefuseReason',
        'refund_money'        => 'require|egt:0.01',
        'refund_freight'      => 'require|egt:0',
        'delivery_company_id' => 'require',
        'delivery_no'         => 'require'
    ];

    /**
     * 提示消息
     */
    protected $message = [
        'id.require'                         => '缺少操作ID',
        'refund_type.require'                => '缺少退款类型',
        'audit_status.require'               => '缺少审核状态',
        'refund_address_id.requireAddressId' => '缺少退货地址',
        'refuse_reason.requireRefuseReason'  => '缺少拒绝原因',
        'refund_money.require'               => '缺少退款金额',
        'refund_money.egt'                   => '退款金额必须大于等于0.01',
        'refund_freight.require'             => '缺少退款运费',
        'refund_freight.egt'                 => '退款运费必须大于等于0',
        'delivery_company_id.require'        => '缺少物流公司ID',
        'delivery_no . require '             => '缺少物流单号',
    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'add'      => [''],
        'edit'     => [''],
        'receive'  => ['id', 'refund_money', 'refund_freight'],
        'exchange' => ['id', 'delivery_company_id', 'delivery_no'],
        'audit'    => ['id', 'audit_status', 'refund_address_id', 'refuse_reason']
    ];

    /**
     * 验证退货地址ID
     */
    protected function requireAddressId($value, $rule, $data = []): bool
    {
        if ($data['audit_status'] == RefundAuditStatus::AUDIT_PASS && empty($value)) {
            return false;
        }

        return true;
    }

    /**
     * 验证拒绝原因
     */
    protected function requireRefuseReason($value, $rule, $data = []): bool
    {
        if ($data['audit_status'] == RefundAuditStatus::AUDIT_REJECT && empty($value)) {
            return false;
        }

        return true;
    }
}
